package com.jy.adscm.operation.mscyb.controller;


import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mscyb.bean.JqzzMaterial;
import com.jy.adscm.operation.mscyb.service.JqzzMaterialService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-06-02
 */
@RestController
@RequestMapping("/adscm/operation/jqzz/jqzzMaterial")
public class JqzzMaterialController {

    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    JqzzMaterialService jqzzMaterialService;
    @RequestMapping("selectByTid/{tid}")
    public SimpleCoreResponse selectByTid(@PathVariable String tid) throws AdscmSystemException {
        List<JqzzMaterial> jqzzMaterials= jqzzMaterialService.selectByTid(tid);
        return  new SimpleCoreResponse("200","查询成功",jqzzMaterials);
    }

    /**
     * 一键领料
     * @param tid
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("approve/{tid}")
    public SimpleCoreResponse approve(@PathVariable String tid, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Boolean result=  jqzzMaterialService.approve(tid,token);
        return  new SimpleCoreResponse("200","领取成功",result);
    }
}

